package com.dlc.felear.xiaoensale.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.dao.MenuTypeDao;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.MenuType;
import com.itdlc.android.library.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *1 公斤菜谱调料值
 */
public class MenuFlavourSettingActivity extends BaseActivity {
    private static final String TAG = "MenuFlavourActivity";
    private TopbarHelper topbarHelper;
    @BindView(R.id.tl_et_jijing)
    EditText etJijing;
    @BindView(R.id.tl_et_tang)
    EditText etTang;
    @BindView(R.id.tl_et_yan)
    EditText etYan;
    @BindView(R.id.tl_et_hjf)
    EditText etHjf;
    @BindView(R.id.tl_et_liaojiu)
    EditText etLiaojiu;
    @BindView(R.id.tl_et_cu)
    EditText etCu;
    @BindView(R.id.tl_et_shengchou)
    EditText etShengchou;
    @BindView(R.id.tl_et_laochou)
    EditText etLaochou;
    @BindView(R.id.tl_et_you)
    EditText etYou;
    @BindView(R.id.tl_et_xiangyou)
    EditText etXiangyou;
    @BindView(R.id.tl_et_haoyou)
    EditText etHaoyou;
    @BindView(R.id.tl_et_shui)
    EditText etShui;
    @BindView(R.id.tl_et_jijing1)
    EditText etJijing1;
    @BindView(R.id.tl_et_tang1)
    EditText etTang1;
    @BindView(R.id.tl_et_yan1)
    EditText etYan1;
    @BindView(R.id.tl_et_hjf1)
    EditText etHjf1;
    @BindView(R.id.tl_et_liaojiu1)
    EditText etLiaojiu1;
    @BindView(R.id.tl_et_cu1)
    EditText etCu1;
    @BindView(R.id.tl_et_shengchou1)
    EditText etShengchou1;
    @BindView(R.id.tl_et_laochou1)
    EditText etLaochou1;
    @BindView(R.id.tl_et_you1)
    EditText etYou1;
    @BindView(R.id.tl_et_xiaogyou1)
    EditText etXiangyou1;
    @BindView(R.id.tl_et_haoyou1)
    EditText etHaoyou1;
    @BindView(R.id.tl_et_shui1)
    EditText etShui1;

    private MenuType menuType;
    private MenuTypeDao menuTypeDao;


    @Override
    protected int getLayoutRes() {
        return R.layout.activity_menu_type_flavour_setting;
    }

    @Override
    protected void initialView() {
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setTitle("菜谱调料默认设置");
        topbarHelper.setcon(R.drawable.arrow_left);
        topbarHelper.setNavActivity(MenuFlavourTypeListActivity.class);
        menuTypeDao=new MenuTypeDao(this);
        initetData();
    }

    @OnClick({R.id.btn_baocun})
    public void clickView(View view) {
        switch (view.getId()) {
            case R.id.btn_baocun:
                baocun();
                break;
        }
    }

    private void baocun() {
        try {
            if(!checkAllEtvalue()){
                showShortToast("设置参数非数字");
                return;
            }
            menuType.setJijing(getEtString(etJijing)+"-"+getEtString(etJijing1));
            menuType.setTang(getEtString(etTang)+"-"+getEtString(etTang1));
            menuType.setYan(getEtString(etYan)+"-"+getEtString(etYan1));
            menuType.setHujiaofen(getEtString(etHjf)+"-"+getEtString(etHjf1));
            menuType.setLiaojiu(getEtString(etLiaojiu)+"-"+getEtString(etLiaojiu1));
            menuType.setCu(getEtString(etCu)+"-"+getEtString(etCu1));
            menuType.setShengchou(getEtString(etShengchou)+"-"+getEtString(etShengchou1));
            menuType.setLaochou(getEtString(etLaochou)+"-"+getEtString(etLaochou1));
            menuType.setYou(getEtString(etYou)+"-"+getEtString(etYou1));
            menuType.setDouyou(getEtString(etXiangyou)+"-"+getEtString(etXiangyou1));
            menuType.setHaoyou(getEtString(etHaoyou)+"-"+getEtString(etHaoyou1));
            menuType.setShui(getEtString(etShui)+"-"+getEtString(etShui1));
            int result=menuTypeDao.update(menuType);
            if(result==1){
                showShortToast("设置成功");
            }else {
                showShortToast("设置失败，请检查设置参数");
            }
            Log.e(TAG,"result="+result);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    private boolean checkAllEtvalue(){
        boolean temp=true;
        if(!checkvalue(etJijing)){
            return false;
        }
        if(!checkvalue(etTang)){
            return false;
        }
        if(!checkvalue(etYan)){
            return false;
        }
        if(!checkvalue(etHjf)){
            return false;
        }
        if(!checkvalue(etLiaojiu)){
            return false;
        }
        if(!checkvalue(etCu)){
            return false;
        }
        if(!checkvalue(etShengchou)){
            return false;
        }
        if(!checkvalue(etLaochou)){
            return false;
        }
        if(!checkvalue(etYou)){
            return false;
        }
        if(!checkvalue(etXiangyou)){
            return false;
        }
        if(!checkvalue(etHaoyou)){
            return false;
        }
        if(!checkvalue(etShui)){
            return false;
        }
        if(!checkvalue(etJijing1)){
            return false;
        }
        if(!checkvalue(etTang1)){
            return false;
        }
        if(!checkvalue(etYan1)){
            return false;
        }
        if(!checkvalue(etHjf1)){
            return false;
        }
        if(!checkvalue(etLiaojiu1)){
            return false;
        }
        if(!checkvalue(etCu1)){
            return false;
        }
        if(!checkvalue(etShengchou1)){
            return false;
        }
        if(!checkvalue(etLaochou1)){
            return false;
        }
        if(!checkvalue(etYou1)){
            return false;
        }
        if(!checkvalue(etXiangyou1)){
            return false;
        }
        if(!checkvalue(etHaoyou1)){
            return false;
        }
        if(!checkvalue(etShui1)){
            return false;
        }
        return temp;
    }

    private void initetData() {
        String id=getIntent().getStringExtra("id");
        menuType=menuTypeDao.queryForId(id);
        if(menuType==null){
            finish();
            return;
        }
        setEtValue(etJijing,etJijing1,menuType.getJijing());
        setEtValue(etJijing,etJijing1,menuType.getJijing());
        setEtValue(etTang,etTang1,menuType.getTang());
        setEtValue(etYan,etYan1,menuType.getYan());
        setEtValue(etHjf,etHjf1,menuType.getHujiaofen());

        setEtValue(etLiaojiu,etLiaojiu1,menuType.getLiaojiu());
        setEtValue(etCu,etCu1,menuType.getCu());
        setEtValue(etShengchou,etShengchou1,menuType.getShengchou());
        setEtValue(etLaochou,etLaochou1,menuType.getLaochou());


        setEtValue(etYou,etYou1,menuType.getYou());
        setEtValue(etXiangyou,etXiangyou1,menuType.getDouyou());
        setEtValue(etHaoyou,etHaoyou1,menuType.getHaoyou());
        setEtValue(etShui,etShui1,menuType.getShui());
        topbarHelper.setTitle(menuType.getName());
    }

    private void setEtValue(EditText et,EditText et1,String value){
        if(value.contains("-")){
            String[] temps=value.split("-");
            if(temps.length>1){
                et.setText(temps[0]);
                et1.setText(temps[1]);
            }
        }else {
            et.setText(value);
        }
    }




    private String getEtString(EditText et) {
        String temp=et.getText().toString().trim();
        if(TextUtils.isEmpty(temp)){
            temp="0";
        }
        boolean tempB=TextUtils.equals(".",Character.toString(temp.charAt(0)));
        if(tempB){
            temp="0"+temp;
        }
        Log.e(TAG,"etValue="+temp);
        return temp;
    }

    private boolean checkvalue(EditText editText){
        String len="[0123456789.]";
        boolean ttt=false;
        String temp=getEtString(editText);
        for(int i=0;i<temp.length();i++){
            ttt= Character.toString(temp.charAt(i)).matches(len);
            if(!ttt){
              break;
            }
        }
        return ttt;
    }

}